package com.iwinding.ladygg.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.event.AddFriendEvent;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.request.AddFriendReq;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.net.response.UserRp;
import com.iwinding.ladygg.utils.CommonUtil;
import com.iwinding.ladygg.utils.RoundedTransformation;
import com.iwinding.ladygg.utils.SystemUtil;
import com.iwinding.ladygg.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by hezhiyong on 2017/4/6.
 */

public class AddContactAdapter extends BaseAdapter<UserRp> {

    LoginRp loginRp=null;

    public AddContactAdapter(Context mContext, List mList) {
        super(mContext, mList);
        loginRp = SystemUtil.getLoginRp(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.icane_adapter_add_contact_item, null);
        }
        final UserRp userRp = mList.get(position);
        final ImageView icon = ViewHolder.get(convertView, R.id.icane_contact_img);
        final TextView name = ViewHolder.get(convertView, R.id.icane_contact_name);
        final Button add = ViewHolder.get(convertView, R.id.icane_add);

        if (!TextUtils.isEmpty(userRp.headImg)) {
            String url = CommonUtil.utf8Togb2312(userRp.headImg);
            Picasso.with(mContext).load(url).transform(new RoundedTransformation(9, 0)).into(icon);
        }
        name.setText(TextUtils.isEmpty(userRp.nickName) ? (TextUtils.isEmpty(userRp.uname) ? "" : userRp.uname) : userRp.nickName);

        add.setVisibility(View.VISIBLE);
        if(loginRp!=null){
            if (TextUtils.equals(loginRp.mobile,userRp.mobile)){
                add.setVisibility(View.GONE);
            }
        }

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent toadd = new Intent(mContext, SendMsgActivity.class);
//                toadd.putExtra(SendMsgActivity.USERNAME,userRp.userId);
//                mContext.startActivity(toadd);
                addFriend(userRp.userId);
            }
        });
        return convertView;
    }

    private void addFriend(final String toAddUsername){
        AddFriendReq req = new AddFriendReq();
        req.userId = toAddUsername;
        RequestManager.postAddFriend(mContext, req, new BaseRpCallback<BaseRopnse>() {
            @Override
            public BaseRopnse parseResponse(BaseRopnse response, int id) {
                return response;
            }

            @Override
            public void onResult(BaseRopnse o) {
                try {
                    EMClient.getInstance().contactManager().addContact(toAddUsername, mContext.getString(R.string.icane_add_user));
                    ToastUtil.openBaseToast(mContext,R.string.icane_add_ok,Toast.LENGTH_SHORT);
                    EventBus.getDefault().post(new AddFriendEvent());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(mContext,errorMsg,Toast.LENGTH_SHORT);
            }

        });
    }
}
